<?php
/*会员管理控制器*/
namespace Admin\Controller;

class MallController extends AdminController {

    /**
     * 商品订单
     */
    public function index()
    {
        $list = M()->query('SELECT
                            a. NAME AS NAME,
                            a.tel AS tel,
                            a.add_1 AS add_1,
                            a.add_2 AS add_2,
                            a.add_3 AS add_3,
                            a.add_4 AS add_4,
                            b. NAME AS goodsname,
                            c.id AS id,
                            c.createtime AS createtime,
                            c.integral AS integral,
                            c.money AS money,
                            c.type AS type
                        FROM
                            onethink_treemember AS a,
                            onethink_goods AS b,
                            onethink_goodsorder AS c
                        WHERE
                            c.g_id = b.id
                        AND c.uid = a.id AND issend=0');
        $count = M()->query('SELECT
                            COUNT(*)
                        FROM
                            onethink_treemember AS a,
                            onethink_goods AS b,
                            onethink_goodsorder AS c
                        WHERE
                            c.g_id = b.id
                        AND c.uid = a.id AND issend=0');
        $page = new \Think\Page($count['0']['COUNT(*)'],10);
        $page ->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->assign('_page',$page->show());
        $this->assign('_list', $list);
        $this->display();
    }

    /**
     * 订单发货
     */
    public function order_send()
    {
        $id = (int)I('id');
        $goodsorder = M('goodsorder')->where(array('id'=>$id))->setInc('issend',1);
        if ($goodsorder === false) {
            $this->error('操作失败');
        } else {
            $this->success('操作成功');
        }

    }
    /**
     * 商品管理
     */
    public function goods_order()
    {
        $list = $this->lists('goods',$map,'isshow ASC,id DESC');
        $this->assign('_list', $list);
        $this->display();
    }
    /**
     * 更改商品状态
     */
    public function changeStatus()
    {
        $method = I('method');
        $id = (int)I('id');
        $goods = M('goods');
        $data = array();
        if ($method == 'down') {
            $data['isshow'] = 2;
        } else if ($method == 'up') {
            $data['isshow'] = 1;
        }
        $res = $goods->data($data)->where('id='.$id)->save();
        if ($res === false) {
            $this->error('设置失败');
        } else {
            $this->success('设置成功');
        }
    }
    /**
     * 提现申请
     */
    public function withdrawals()
    {
        $ExchangeView = D('ExchangeView');
        $map['ispay'] = 1;
        $list = $ExchangeView->where($map)->select();
        $count = $ExchangeView->where($map)->count();
        $page = new \Think\Page($count,10);
        $page ->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->assign('_page',$page->show());
        $this->assign('_list', $list);
        $this->display();
    }
    /**
     * 提现转账操作
     */
    public function take_pay()
    {
        $id = 0;
        $Exchange = M('Exchange');
        $res = $Exchange->where('id='.$id)->setDec('ispay');
        if ($res === false || $res === 0) {
            $this->error('操作失败');
        } else {
            $this->success('操作成功');
        }
    }
    /**
     * 商品添加
     */
    public function add()
    {
        if (IS_POST) {
            $goods = M('goods');
            $goodspic = M('goodspic');
            $data = $_POST;
            $pic = $_POST['pic'];
            unset($data['pic']);
            $data['createtime'] = time();
            $data['cuid'] = session('user_auth.uid');
            $res = $goods->data($data)->add();
            if ($res === false) {
                $this->error('添加失败');
            } else {
                foreach ($pic as $v) {
                    $goodspic->add(array('g_id'=>$res,'path'=>$v));
                }
                $this->success('添加成功');
            }
        } else {
            $this->display();
        }
    }
    /**
     * 商品编辑
     */
    public function edit()
    {
        $goods = M('goods');
        $goodspic = M('goodspic');
        if (IS_GET) {
            $id = I('id');
            $info = $goods->find((int)$id);
            $this->assign('info',$info);
            $pic = $goodspic->where(array('g_id'=>(int)$id))->select();
            $picimg = '';
            foreach ($pic as $k => $v) {
                $picc[$k] = $v['path'];
                $picimg .= "<img src='".$v['path']."'  width='200' height='200'/>";
            }
            $this->assign('pic',$picc);
            $this->assign('picimg',$picimg);
            $this->display();
        } else {
            $data = $_POST;
            $id = (int)I('id');
            $pic = $data['pic'];
            unset($data['pic']);
            unset($data['id']);
            $res = $goods->data($data)->where('id='.$id)->save();
            foreach ($pic as $k => $v) {
                $goodspic->add(array('g_id'=>$id,'path'=>$v));
            }
            if ($res === false) {
                $this->error('编辑失败');
            } else {
                $this->success('编辑成功');
            }
        }
    }
}